﻿@inject IDataCacheService _dataCacheService
@inject IHttpService _httpService

@inject NavigationManager NavigationManager

<CnGalWebSite.Shared.MasaComponent.Shared.Cards.TitleTip Title="动态汇总"/>
<style>
    .accordion-body {
        padding: 0;
    }
</style>

<div>
    <MDCard Class="mb-4">
        <MTabs Value="@_dataCacheService.NewsSummaryCache.TabIndex" ValueChanged="ValueChanged" IconsAndText Grow CenterActive>
             <MTab Value="1" OnClick="@(() => OnCondited("本周"))">
                 本周<MIcon>mdi-av-timer </MIcon>
             </MTab>
             <MTab Value="2" OnClick="@(() => OnCondited("本月"))">
                 本月<MIcon>mdi-clock-outline</MIcon>
             </MTab>
             <MTab Value="4" OnClick="@(() => OnCondited("本年"))">
                 本年<MIcon>mdi-calendar-text </MIcon>
             </MTab>
         </MTabs>
    </MDCard>

    <div class="mt-4">
        @if (isReady)
        {

            if (_dataCacheService.NewsSummaryCache.Items.Count == 0)
            {
                <NotFoundCard Fill />
            }
            else
            {
                @foreach (var item in _dataCacheService.NewsSummaryCache.Items.Skip((_dataCacheService.NewsSummaryCache.CurrentPage - 1) * _dataCacheService.NewsSummaryCache.MaxCount).Take(_dataCacheService.NewsSummaryCache.MaxCount))
                {

                    <div class="mb-4">
                        <NewsSummaryAloneViewTip Model="item" />

                    </div>
                }

                @if (_dataCacheService.NewsSummaryCache.TotalPages > 1)
                {
                    <CnGalWebSite.Components.Paginations.MasaPagination Value="@_dataCacheService.NewsSummaryCache.CurrentPage" Length="@_dataCacheService.NewsSummaryCache.TotalPages" ValueChanged="OnPageClick"/>
                }
            }
        }
        else
        {
            <CnGalWebSite.Components.Progressings.ProgressingCard Page Fill />
        }

    </div>


</div>


@code {

    bool isReady = false;

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    private void OnPageClick(int pageIndex)
    {
        _dataCacheService.NewsSummaryCache.CurrentPage = pageIndex;
        StateHasChanged();

    }

    protected override async Task OnInitializedAsync()
    {
        if (_dataCacheService.NewsSummaryCache.Items.Count == 0)
        {
            await OnCondited("本周");
        }

        isReady = true;
    }

    private async Task OnCondited(string name)
    {
        isReady = false;
        StateHasChanged();
        try
        {
            _dataCacheService.NewsSummaryCache.Items = await _httpService.GetAsync<List<NewsSummaryAloneViewModel>>("api/articles/GetNewSummary/" + name);
            _dataCacheService.NewsSummaryCache.CurrentPage = 1;
            isReady = true;
            StateHasChanged();
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "获取动态分类汇总列表失败");
        }
    }

    public void ValueChanged(StringNumber value)
    {
        _dataCacheService.NewsSummaryCache.TabIndex = value.ToInt32();
    }
}

